package com.jlcindia.action.classes;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jlc.connPage.JDBCUtil;

public class LoginAction {
	
	public LoginAction(){
		System.out.println("LoginAction() Cons Invoked, means object is created");	
	}
	Connection con=null;
	PreparedStatement ps=null;
	ResultSet rs=null;

	public String verifyUser(HttpServletRequest request,
			HttpServletResponse response) {
		System.out.println("~~~~~~verifyUser() method invoked~~~~~");
		String name = request.getParameter("name");
		String pwd = request.getParameter("password");
		String page = "";		
		try {
			con=JDBCUtil.getMysqlConnection();
			ps=con.prepareStatement("select name,password from userInfo where name=? and password=?");
			ps.setString(1, name);
			ps.setString(2, pwd);
			rs=ps.executeQuery();
			if(rs.next()){
				page = "home.jsp";
			}else{
				request.setAttribute("ERROR", "Username or Password is Invalid");
				page = "login.jsp";
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			JDBCUtil.cleanup(con, ps, rs);
		}
		return page;

	}

}
